const server = require("../../../utils/server");

// pages/dish/cart/cart.js
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    cartList: null,
    storeName: '',
    totalPrice: 0,
    allSelected: true
  },

  addNumber(e) {
    let id = e.currentTarget.dataset.id;
    let list = this.data.cartList;
    list.forEach((item, index) => {
      if (item._id == id) {
        item.salesNumber++;
      } else {
        return
      }
    });
    this.setData({
      cartList: list
    });
    this.getTotal(list);
  },

  lessNumber(e) {
    let index = e.currentTarget.dataset.index;
    let list = this.data.cartList;
    let that = this;
    if (list[index].salesNumber == 1) {
      wx.showModal({
        content: "确定删除此商品吗？",
        cancelText: "取消",
        confirmText: "确认",
        success(res) {
          if (res.confirm) {
            list.splice(index, 1);
          } else {
            return
          };
          that.setData({
            cartList: list
          });
          that.getTotal(list);
        }
      })
    } else {
      list[index].salesNumber--
    }
    that.setData({
      cartList: list
    });
    that.getTotal(list);
  },

  getTotal(list) {
    let totalPrice = 0;
    if (list.length) {
      list.forEach((item, index) => {
        if (item.checked == true) {
          totalPrice += item.salePrice * item.salesNumber;
        }
      })
    };
    this.setData({
      totalPrice: totalPrice
    })
  },

  getCartlist() {
    server.requestForm(true, 'get', 'cart', {},
      function (res) {
        if (res.code == 0) {

        } else(
          wx.showToast({
            title: res.message.message,
            icon: 'none',
            duration: 1000
          })
        )
      }
    )
  },

  selected(e) {
    let index = e.currentTarget.dataset.index;
    let list = this.data.cartList;
    let flag = list[index].checked;
    list[index].checked = !flag;
    this.setData({
      cartList: list
    });
    this.getTotal(list)
  },

  selectAll() {
    let flag = this.data.allSelected;
    let list = this.data.cartList;
    if (flag == true) {
      list.forEach((item, index) => {
        item.checked = false
      });
      this.setData({
        cartList: list,
        allSelected: false
      })
      this.getTotal(list)
    } else {
      list.forEach((item, index) => {
        item.checked = true
      });
      this.setData({
        cartList: list,
        allSelected: true
      });
      this.getTotal(list)
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let storeName = wx.getStorageSync('storeName');
    let cartList = wx.getStorageSync('cartList');
    cartList.forEach((item, index) => {
      item.checked = true
    });
    this.getTotal(cartList);
    this.setData({
      storeName: storeName,
      cartList: cartList
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let tabbar = this.selectComponent("#tabbar");
    tabbar.setData({
      selected: 2
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})